#!/bin/bash 
set -e


TMP=${TMP:-/tmp/work}
LOG=${LOG:-$HOME/logs/stage}
SRC=${SRC:-$LFS/sources}

name=strippingagain

rm -rf $TMP
mkdir -p $TMP $LOG


{ time \
   {

	save_lib="ld-2.33.so libc-2.33.so libpthread-2.33.so libthread_db-1.0.so"

	cd /lib

	for LIB in $save_lib; do
	    objcopy --only-keep-debug $LIB $LIB.dbg 
	    strip --strip-unneeded $LIB
	    objcopy --add-gnu-debuglink=$LIB.dbg $LIB 
	done    

	save_usrlib="libquadmath.so.0.0.0 libstdc++.so.6.0.28
	             libitm.so.1.0.0 libatomic.so.1.2.0" 

	cd /usr/lib

	for LIB in $save_usrlib; do
	    objcopy --only-keep-debug $LIB $LIB.dbg
	    strip --strip-unneeded $LIB
	    objcopy --add-gnu-debuglink=$LIB.dbg $LIB
	done

	unset LIB save_lib save_usrlib
	    
	find /usr/lib -type f -name \*.a \
	   -exec strip --strip-debug {} ';'

	find /lib /usr/lib -type f -name \*.so* ! -name \*dbg \
	   -exec strip --strip-unneeded {} ';'

	find /{bin,sbin} /usr/{bin,sbin,libexec} -type f \
	    -exec strip --strip-all {} ';'

	rm -rf /tmp/*

    }
} 2>&1 | tee $name.log
    
[ $PIPESTATUS = 0 ] && mv $name.log $LOG || exit $PIPESTATUS

rm -fr $TMP
